//
//  JZCameraShootingButton.m
//  JZShortVideo
//
//  Created by liujz on 31/05/2018.
//  Copyright © 2018 liujz. All rights reserved.
//

#import "JZCameraShootingButton.h"
#import "JZCameraShootingCenterButton.h"
#import "ColorManager.h"
#import <Masonry/Masonry.h>

@interface JZCameraShootingButton () <shootingCenterDelegate>

@property (nonatomic, strong) JZCameraShootingCenterButton *centerButton;
@property (nonatomic, strong) CAShapeLayer  *progressLayer;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) CGFloat       progressValue;

@end

@implementation JZCameraShootingButton

+ (JZCameraShootingButton *)getShootingButtonWithTapEnable:(BOOL)tapEnable LongPressEnable:(BOOL)longPressEnable {
    
    JZCameraShootingButton *shootingButton = [[JZCameraShootingButton alloc] init];
    
    // 进度圈内部按钮
    JZCameraShootingCenterButton *centerButton = [JZCameraShootingCenterButton getShootingCenterButton:20 minLineWidth:10 lineColor:nil centerColor:nil TapEnable:tapEnable LongPressEnable:longPressEnable];
    
    shootingButton.centerButton         = centerButton;
    centerButton.shootingCenterDelegate = shootingButton;
    [shootingButton addSubview:centerButton];

    [centerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(9, 9, 9, 9));
    }];
    
    // 进度圈
    shootingButton.progressLayer               = [CAShapeLayer layer];
    shootingButton.progressLayer.strokeColor   = [ColorManager colorWithRGB:@"#FD5729"].CGColor;  // 设置划线颜色
    shootingButton.progressLayer.fillColor     = [UIColor clearColor].CGColor;                       // 设置填充颜色
    shootingButton.progressLayer.lineWidth     = 3; // 设置线宽
    shootingButton.progressLayer.strokeStart   = 0;
    shootingButton.progressLayer.strokeEnd     = 0; // 设置轮廓结束位置
    
    // 旋转到垂直方向
    CATransform3D turnTrans = CATransform3DMakeRotation(-M_PI / 2, 0, 0, 1);
    shootingButton.progressLayer.transform = turnTrans;
    [shootingButton.layer addSublayer:shootingButton.progressLayer];
    return shootingButton;
    
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.progressLayer.frame    = self.bounds;
    self.progressLayer.path     = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath; // 设置绘制路径

}

#pragma mark - Shooting Center Delegate

/**
 *开始录制或者拍照
 *
 * @param actionType 判断拍照还是录制类型
 */
- (void)shootingStart:(TapType)actionType obj:(JZCameraShootingCenterButton *)button {
    
    // 长按的手势（开启定时器）
    if (LongPressGesture == actionType) {
         self.progressLayer.strokeColor = [UIColor greenColor].CGColor;
         self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeProgerss)];
         [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }

    // 回调自己的代理(开始)
    if ([self.shootingButtonrDelegate respondsToSelector:@selector(shootingStarting:shootingType:progress:)]) {
        [self.shootingButtonrDelegate shootingStarting:self shootingType:actionType progress:self.progressValue];
    }
}

/**
 录制或者拍照结束的回调
 
 @param action 判断拍照还是录制类型
 */
- (void)shootingEnd:(TapType)action obj:(JZCameraShootingCenterButton *)button {
    
    if (LongPressGesture == action) {
        [self stopProress];
    }
    
    // 回调自己的代理(结束)
    if ([self.shootingButtonrDelegate respondsToSelector:@selector(shootingStop:shootingType:)]) {
        [self.shootingButtonrDelegate shootingStop:self shootingType:action];
    }
    
}

#pragma mark - Action 事件处理逻辑

/**
 修改进度要的状态（定时器实时调用的方法）
 */
- (void)changeProgerss {
    
    double speed = (1.0 / 10.0) / 60.0;     // 拍摄的时间
    self.progressValue = self.progressValue + speed;
    self.progressLayer.strokeEnd = self.progressValue;
    if (self.progressValue >= 1.05) {
        [self stopProress];
    }
    
    //回调自己的代理(开始)
    if ([self.shootingButtonrDelegate respondsToSelector:@selector(shootingStarting:shootingType:progress:)]) {
        [self.shootingButtonrDelegate shootingStarting:self shootingType:LongPressGesture progress:self.progressValue];
    }
}

/**
 结束视频的拍摄
 */
- (void)stopProress {
    
    self.progressValue = 0;
    self.progressLayer.strokeEnd    = self.progressValue;
    self.progressLayer.strokeColor  = [UIColor clearColor].CGColor;
    [self.displayLink invalidate];
}


@end



